﻿using System;
using System.Collections.Generic;
using System.Text;
using DataAccess.Data;
using System.Data;

using hrModel;
using System.IO;

namespace hrBll
{
    public class BLLPicture
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static int Add(Picture model)
        {
            string newFileName = model.PicUrl;
            if (model.PicUrl.Trim().Length > 0)
            {
                newFileName =model.EmpID.ToString() + new FileInfo(model.PicUrl).Extension;
            } 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Picture(");
            strSql.Append("PicDesn,PicUrl,PicDate,PriUser,EmpID");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.PicDesn + "',");
            strSql.Append("'" + newFileName+"',");
            strSql.Append("'" + model.PicDate + "',");
            strSql.Append("'" + model.PriUser + "',");
            strSql.Append("" + model.EmpID + "");
            strSql.Append(")");
            int rt = DbHelperSQL.Instance.ExcuteNonQuery(strSql.ToString());
            if (rt>0)
            {
                //复制图片
                if (model.PicUrl.Trim().Length > 0)
                {
                    ImageManage.CopyImageFileToServer(model.PicUrl,newFileName);
                }
            }
            return rt;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static int Update(Picture model)
        {
            string newFileName = model.PicUrl;
            if (model.PicUrl.Trim().Length > 0)
            {
                newFileName = model.EmpID.ToString() + new FileInfo(model.PicUrl).Extension;
            } 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Picture set ");
            strSql.Append("PicDesn='" + model.PicDesn + "',");
            strSql.Append("PicUrl='" + newFileName + "',");
            strSql.Append("PriUser='" + model.PriUser + "'");
            strSql.Append(" where PictureID=" + model.PictureID + "");
            int rt = DbHelperSQL.Instance.ExcuteNonQuery(strSql.ToString());
            if (rt > 0)
            {
                //复制图片
                if (model.PicUrl.Trim().Length > 0)
                {
                    ImageManage.CopyImageFileToServer(model.PicUrl, newFileName);
                }
            }
            return rt;
        }

        public static bool CheckPicUrl(string PicUrl,string PictureID)
        {
            string strUrl = DbHelperSQL.Instance.ExecuteScalar("select PicUrl from Picture where PictureID = " + PictureID);
            if (!PicUrl.Equals(PicUrl))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static int Delete(int PictureID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete Picture ");
            strSql.Append(" where PictureID = " + PictureID);
            return DbHelperSQL.Instance.ExcuteNonQuery(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Picture GetPicture(int PictureID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from Picture ");
            strSql.Append(" where EmpID=" + PictureID);
            Picture model = new Picture();
            DataTable dt = DbHelperSQL.Instance.GetDataTable(strSql.ToString());
            model.PictureID = PictureID;
            if (dt.Rows.Count > 0)
            {
                model.PicDesn = dt.Rows[0]["PicDesn"].ToString();
                model.PicUrl = dt.Rows[0]["PicUrl"].ToString();
                if (dt.Rows[0]["PicDate"].ToString() != "")
                {
                    model.PicDate = DateTime.Parse(dt.Rows[0]["PicDate"].ToString());
                }
                model.PriUser = dt.Rows[0]["PriUser"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        public static string GetPicUrlByPicID(string PicID)
        {
            return DbHelperSQL.Instance.ExecuteScalar("Select PicUrl from Picture where PictureID = " + PicID);
        }
    }
}
